package development.app.football;

import state.AlertListener;
import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.component.Event;
import state.component.EventListener;
import state.component.MultiTab;
import state.font.FontManager;
import state.font.ImageText;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.GameBoolean;
import state.util.array.StringArray;
import development.Constants;
import development.DrawUtils;
import development.FrmGuide;
import development.Global;
import development.app.FrmDanhSachNguoiChoi;
import development.app.FrmDauGiaNguoc;
import development.app.FrmLottery;
import development.app.FrmSuKienHot;
import development.group.FrmGroupBongDaXoSo;
import development.group.FrmGroupNapXuAndExchangeGold;
import development.group.FrmGroupThoiTietTinTuc;

public class FrmBettingMenu extends GameForm implements EventListener, AlertListener {
  private int HOZ_DISTANCE;
  private int VER_DISTANCE;
  private int DX;
  private int DY;
  private int ITEM_WIDTH;
  private int ITEM_PER_ROW;

  private GameImage[] itemsImages;

  private ImageText text8;

  private int chooseIndex;
  private int numberOfItem;

  private String[] menuText;
  private int countRow;

  private Hashtable parameters;

  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmBettingMenu();
    }
    return singleton;
  }

  public void init(Hashtable parameters1) {
    this.parameters = parameters1;
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    numberOfItem = 4;

    menuText = new String[] { "Vào chơi", "Lịch sử chơi", "Nạp xu", "Hướng dẫn" };
    itemsImages = new GameImage[numberOfItem];
    itemsImages[0] = ImageUtil.getImage(Constants.GAME_MENU_ICON_CATEGORY, "icon_Vao-phong.png");
    itemsImages[1] = ImageUtil.getImage(Constants.GAME_MENU_ICON_CATEGORY, "icon_Tu-tim-phong.png");
    itemsImages[2] = ImageUtil.getImage(Constants.GAME_MENU_ICON_CATEGORY, "icon_Danh-sach.png");
    itemsImages[3] = ImageUtil.getImage(Constants.GAME_MENU_ICON_CATEGORY, "icon_Huong-dan.png");

    if (Constants.IS_240x320_SCREEN) {
      HOZ_DISTANCE = 0;
      VER_DISTANCE = 23;
      DX = 50;
      DY = 100;
      ITEM_WIDTH = 142;
      ITEM_PER_ROW = 1;
    } else {
      HOZ_DISTANCE = 0;
      VER_DISTANCE = 23;
      DX = 90;
      DY = 50;
      ITEM_WIDTH = 142;
      ITEM_PER_ROW = 1;
    }
    if (numberOfItem % ITEM_PER_ROW == 0) {
      countRow = numberOfItem / ITEM_PER_ROW;
    } else {
      countRow = numberOfItem / ITEM_PER_ROW + 1;
    }

    DrawUtils.setBottomBar(_manager, "", "Chọn", "Trở về");
    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    g.setColor(0x6B0101);
    g.fillRect(DX, DY, (ITEM_WIDTH + HOZ_DISTANCE) * ITEM_PER_ROW, countRow * VER_DISTANCE + 3);
    for (int i = 0; i < numberOfItem; i++) {
      int x = DX + i % ITEM_PER_ROW * (HOZ_DISTANCE + ITEM_WIDTH);
      int y = DY + i / ITEM_PER_ROW * VER_DISTANCE;

      if (chooseIndex == i) {
        g.setColor(0x2E772E);
        g.fillRect(x + 2, y + 2, ITEM_WIDTH - 3, VER_DISTANCE);
        text8.drawString2(g, menuText[i], 0xDDFD52, x + 33, y + 5, GameConstants.TOP_LEFT_ANCHOR);
      } else {
        text8.drawString2(g, menuText[i], 0xF3DE34, x + 33, y + 5, GameConstants.TOP_LEFT_ANCHOR);
      }
      g.drawImage(itemsImages[i], x + 7, y + 4, Constants.TOP_LEFT_ANCHOR);
    }
    g.setColor(0xB45454);
    g.drawRect(DX, DY, (ITEM_WIDTH + HOZ_DISTANCE) * ITEM_PER_ROW, countRow * VER_DISTANCE + 3);
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.DOWN:
      chooseIndex = (chooseIndex + ITEM_PER_ROW > numberOfItem - 1) ? chooseIndex : chooseIndex + ITEM_PER_ROW;
      break;
    case Key.UP:
      chooseIndex = (chooseIndex - ITEM_PER_ROW < 0) ? chooseIndex : chooseIndex - ITEM_PER_ROW;
      break;
    case Key.FIRE:
      //"Vào chơi", "Lịch sử chơi", "Nạp xu", "Hướng dẫn";
      if (chooseIndex == 0) {
        if (parameters == null) {
          return;
        }
        String requestType = (String) parameters.get("RequestType");
        if (requestType == null || requestType.equals("")) {
          return;
        }
        
        if (requestType.equals("FOOTBALL")) {
          GameBoolean requestVotingObject = (GameBoolean) parameters.get("requestVotingObject");
          if (requestVotingObject.booleanValue()) {
            FrmBongDaBetting.backTab = this;
            FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
            frmGroupBongDaXoSo.getMultiTab().setTab(1, FrmBongDaBetting.instance(), parameters);
          } else {
            Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Chương trình đã kết thúc", "bạn không thể vào chơi" ));
          }
        } else if (requestType.equals("PHANG_ONLINE")) {
          if (parameters.get("status") != null && parameters.get("status").equals("1")) {
            FrmGroupBongDaXoSo.tabIndex = 0;
            FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
            frmGroupBongDaXoSo.getMultiTab().setTab(0, FrmLottery.instance(), parameters);
          } else {
            Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Chương trình đã kết thúc", "bạn không thể vào chơi" ));
          }
        } else if (requestType.equals("DAU_GIA_NGUOC")) {
          String status = (String) parameters.get("status");
          if (status.equals("1")) {
            parameters.put("requestVotingObject", new GameBoolean(true));
            if (parentGroup instanceof FrmGroupBongDaXoSo) {
              FrmDauGiaNguoc.backTab = this;
              FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
              frmGroupBongDaXoSo.getMultiTab().setTab(2, FrmDauGiaNguoc.instance(), parameters);
            } else if (parentGroup instanceof  FrmGroupThoiTietTinTuc) {
              FrmDauGiaNguoc.backTab = this;
              FrmGroupThoiTietTinTuc frmGroupThoiTietTinTuc = (FrmGroupThoiTietTinTuc) FrmGroupThoiTietTinTuc.instance();
              frmGroupThoiTietTinTuc.getMultiTab().setTab(2, FrmDauGiaNguoc.instance(), parameters);
            }
          } else {
            Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Chương trình đã kết thúc", "bạn không thể vào chơi" ));
          }
        }
        return;
      }
      if (chooseIndex == 1) {
        String requestType = (String) parameters.get("RequestType");
        if (requestType == null || requestType.equals("")) {
          return;
        }
        Global.nextState(FrmDanhSachNguoiChoi.instance(), parameters);
        return;
      }
      if (chooseIndex == 2) {
        if (parentGroup != null) {
          FrmGroupNapXuAndExchangeGold.backForm = parentGroup;
        } else {
          FrmGroupNapXuAndExchangeGold.backForm = this;
        }
        Global.nextState(FrmGroupNapXuAndExchangeGold.instance(), parameters);
      }
      if (chooseIndex == 3) {
        String requestType = (String) parameters.get("RequestType");
        String helpId = "iplay";
        if (requestType.equals("DAU_GIA_NGUOC")) {
          helpId = "DauGiaNguoc";
        } else if (requestType.equals("PHANG_ONLINE")) {
          helpId = "BetXoSo";
        } else if (requestType.equals("FOOTBALL")) {
          helpId = "BetBongDa";
        }
        parameters.put("helpId", helpId);
        FrmGuide.backForm = FrmGroupBongDaXoSo.instance();
        GameGlobal.nextState(FrmGuide.instance(), parameters);
      }
      break;
    case Key.SOFT_RIGHT:
      String requestType = (String) parameters.get("RequestType");
      if (requestType.equals("PHANG_ONLINE")) {
        parameters.put("formType", "ShowFormBettingBrandList");
        FrmGroupBongDaXoSo.tabIndex = 0;
        FrmLottery.instance().detroyAll();
        FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
        frmGroupBongDaXoSo.getMultiTab().setTab(0, FrmLottery.instance(), null);
        FrmGroupBongDaXoSo.setTab(0);
      } else if (requestType.equals("DAU_GIA_NGUOC")) {
        if (parameters.get("requestVotingObject") != null) {
          parameters.remove("requestVotingObject");
        }
        FrmDauGiaNguoc.instance().detroyAll();
        FrmDauGiaNguoc.backTab = FrmSuKienHot.instance();
        if (parentGroup instanceof FrmGroupBongDaXoSo) {
          FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
          frmGroupBongDaXoSo.getMultiTab().setTab(2, FrmDauGiaNguoc.instance(), parameters);
        } else if (parentGroup instanceof  FrmGroupThoiTietTinTuc) {
          FrmGroupThoiTietTinTuc frmGroupThoiTietTinTuc = (FrmGroupThoiTietTinTuc) FrmGroupThoiTietTinTuc.instance();
          frmGroupThoiTietTinTuc.getMultiTab().setTab(2, FrmDauGiaNguoc.instance(), null);
        }
      } else if (requestType.equals("FOOTBALL")) {
        FrmBongDaBetting.instance().detroyAll();
        parameters.remove("phaseId");
        parameters.remove("requestVotingObject");
        FrmBongDaBetting.backTab = FrmBongDa.instance();
        FrmGroupBongDaXoSo frmGroupBongDaXoSo = (FrmGroupBongDaXoSo) FrmGroupBongDaXoSo.instance();
        frmGroupBongDaXoSo.getMultiTab().setTab(1, FrmBongDaBetting.instance(), parameters);
      }
      break;
    }
  }

  public void onActionPerform(Event event) {
    if (MultiTab.OPEN_TAB_EVENT.equals(event.getAction())) {
      DrawUtils.setBottomBar(_manager, "", "Chọn", "Trở về");
      return;
    }
  }

  public void destroy() {
    itemsImages = null;
    text8 = null;
    menuText = null;
    parameters = null;
    chooseIndex = 0;
  }
}
